package com.ggh.jinjilive.ui.social.message.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemMessageBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.utils.im.DateTimeUtil;
import com.ggh.jinjilive.utils.im.MessageInfo;
import com.bumptech.glide.Glide;
import com.tencent.imsdk.v2.V2TIMConversation;
import com.tencent.imsdk.v2.V2TIMMessage;

import java.util.Date;
import java.util.List;

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ItemViewHolder> {
    private Context mContext;
    public MessageAdapter(Context mContext){
        this.mContext = mContext;
    }

    private List<V2TIMConversation> v2TIMConversationList;

    public void ADD(List<V2TIMConversation> v2TIMConversationList){
        this.v2TIMConversationList = v2TIMConversationList;
        notifyDataSetChanged();
    }

    public void setAdd(List<V2TIMConversation> v2TIMConversationList){
        if(null != this.v2TIMConversationList){
            this.v2TIMConversationList.addAll(v2TIMConversationList);
        }else {
            this.v2TIMConversationList = v2TIMConversationList;
        }
        notifyDataSetChanged();
    }

    public void setADD(List<V2TIMConversation> v2TIMConversationList){
        if(null != this.v2TIMConversationList){
            this.v2TIMConversationList.addAll(v2TIMConversationList);
        }else {
            this.v2TIMConversationList = v2TIMConversationList;
        }
        notifyDataSetChanged();
    }


    private AddItemOnClickListener addItemOnClickListener;
    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener) {
        this.addItemOnClickListener = addItemOnClickListener;
    }



    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemMessageBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_message, parent, false);
        return new ItemViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        V2TIMConversation v2TIMConversation = v2TIMConversationList.get(position);
        if(null != v2TIMConversation){
            if(position == 0){
                holder.binding.llMessage.setPadding(SizeUtils.dp2px(5), SizeUtils.dp2px(5),SizeUtils.dp2px(5),0);
            }else if(position == getItemCount()){
                holder.binding.llMessage.setPadding(SizeUtils.dp2px(5), 0,0,SizeUtils.dp2px(5));
            }else {
                holder.binding.llMessage.setPadding(SizeUtils.dp2px(5), 0,SizeUtils.dp2px(5),0);
            }


            V2TIMMessage v2TIMMessage = v2TIMConversation.getLastMessage();
            //头像
            Glide.with(holder.binding.imgPortrait).load(v2TIMConversation.getFaceUrl()).into(holder.binding.imgPortrait);
            //用户名称
            holder.binding.conversationTitle.setText(v2TIMConversation.getShowName());
            //时间
            holder.binding.conversationTime.setText(DateTimeUtil.getTimeFormatText(new Date(v2TIMMessage.getTimestamp() *1000)));
            //撤回了
            if (v2TIMMessage != null && v2TIMMessage.getStatus() == MessageInfo.MSG_STATUS_REVOKE) {
                if (v2TIMMessage.isSelf()) {
                    //消息
                    holder.binding.conversationLastMsg.setText("您撤回了一条消息");
                }else {
                    holder.binding.conversationLastMsg.setText("对方撤回了一条消息");
                }
            }else {
                //textElem 会存储文本消息内容
                if(v2TIMMessage.getElemType() == V2TIMMessage.V2TIM_ELEM_TYPE_TEXT){
                    holder.binding.conversationLastMsg.setText(v2TIMMessage.getTextElem().getText());
                }else if(v2TIMMessage.getElemType() == V2TIMMessage.V2TIM_ELEM_TYPE_IMAGE){//会存储图片消息内容
                    holder.binding.conversationLastMsg.setText("[图片消息]");
                }else if(v2TIMMessage.getElemType() == V2TIMMessage.V2TIM_ELEM_TYPE_SOUND){//会存储语音消息内容
                    holder.binding.conversationLastMsg.setText("[语音消息]");
                }else if(v2TIMMessage.getElemType() == V2TIMMessage.V2TIM_ELEM_TYPE_VIDEO){//会存储视频消息内容
                    holder.binding.conversationLastMsg.setText("[视频消息]");
                }else if(v2TIMMessage.getElemType() == V2TIMMessage.V2TIM_ELEM_TYPE_VIDEO){//会存储文件消息内容
                    holder.binding.conversationLastMsg.setText("[文件消息]");
                } else if (v2TIMMessage.getElemType() == V2TIMMessage.V2TIM_ELEM_TYPE_FILE) {
                    holder.binding.conversationLastMsg.setText("[文件消息]");
                }
            }
            //消息未读数
            if (v2TIMConversation.getUnreadCount() > 0 ) {
                holder.binding.conversationUnread.setVisibility(View.VISIBLE);
                if (v2TIMConversation.getUnreadCount() > 99) {
                    holder.binding.conversationUnread.setText("99+");
                } else {
                    holder.binding.conversationUnread.setText("" + v2TIMConversation.getUnreadCount());
                }
            } else {
                holder.binding.conversationUnread.setVisibility(View.GONE);
            }
            holder.binding.llMessage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(null != addItemOnClickListener){
                        addItemOnClickListener.onSkipClicked(position,v2TIMMessage);
                    }
                }
            });

        }
    }

    @Override
    public int getItemCount() {
        return null == v2TIMConversationList ? 0 : v2TIMConversationList.size();
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemMessageBinding binding;

        public ItemViewHolder(@NonNull ItemMessageBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}